﻿using System;
using System.Collections;
using System.Reflection;
using log4net;
using log4net.Config;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.ServiceModel;
using System.ComponentModel;
using System.ServiceModel.Security;
using System.Collections.Generic;
using System.ServiceModel.Description;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


namespace HaiFang
{
    [Serializable]
    public class ProxyProjectInfo
    {
        private static string PROCEDURE_QueryProjectInfo = "QueryProjectInfo";
        private static string PROCEDURE_InsertProjectInfo = "InsertProjectInfo";

        //@CurrUserID			int = null,					--管理员编号
        //@AutoID				bigint=0					--查询所有时传0，否则传编号值
        public static IList<ProjectInfo> QueryProjectInfo(int CurrUserID, int AutoID)
        {
            IList<ProjectInfo> list = new List<ProjectInfo>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryProjectInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<ProjectInfo>(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }

        //@CurrUserID			int = null,					--管理员编号
        //@AutoID				bigint=0					--查询所有时传0，否则传编号值
        public static IList<ProjectInfoBase> QueryProjectInfoSingle(int CurrUserID, int AutoID)
        {
            IList<ProjectInfoBase> list = new List<ProjectInfoBase>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryProjectInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<ProjectInfoBase>(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }

        /// <summary>
        /// 找出当前选中的项目
        /// </summary>
        /// <param name="CurrUserID"></param>
        /// <param name="AutoID"></param>
        /// <param name="SelectFlag"></param>
        /// <returns></returns>
        public static ProjectInfo QueryProjectInfo(int CurrUserID, int AutoID, int SelectFlag)
        {
            ProjectInfo r = null;
            try
            {
                IList<ProjectInfo> list = QueryProjectInfo(CurrUserID, AutoID);
                if (list != null && list.Count > 0)
                {
                    foreach (ProjectInfo pi in list)
                    {
                        if (pi.SelectFlag == SelectFlag)
                        {
                            r = pi;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return r;
        }

        //@CurrUserID			int = null,					--管理员编号
        //@UseFlag			tinyint,					--1:insert 2:update   3:delete
        //@AutoID				bigint=0,					--@UseFlag为1时传0，其它操作传编号值
        //@ProjectName		varchar(200),
        //@SelectFlag			tinyint=2,					--选择标志：1选择   2非选择
        //@ProjectDisk			varchar(10),			--盘符
        //@Description		varchar(128),
        //@ErrorMSG			int	output					--100：项目存在 --200：盘符存在 0：正确；其他：错误		
        public static void InsertProjectInfo(int CurrUserID, int UseFlag, int AutoID, string ProjectName, int SelectFlag, string ProjectDisk, string Description, out int ErrorMSG)
        {
            ErrorMSG = -1;
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_InsertProjectInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@UseFlag", UseFlag));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    Parameters.Add(new SqlParameter("@ProjectName", ProjectName));
                    Parameters.Add(new SqlParameter("@SelectFlag", SelectFlag));
                    Parameters.Add(new SqlParameter("@ProjectDisk", ProjectDisk));
                    Parameters.Add(new SqlParameter("@Description", Description));
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@ErrorMSG",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = ErrorMSG
                    });
                    #endregion

                    #region 获取执行结果
                    helper.Execute(comandText, CommandType.StoredProcedure, Parameters);
                    #endregion

                    #region 获取output值
                    foreach (SqlParameter sp in Parameters)
                    {
                        if (sp.ParameterName == "@ErrorMSG")
                        {
                            ErrorMSG = helper.GetInt(sp.Value);
                            break;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
        }
    }

    [Serializable]
    public class ProjectInfo : ProjectInfoBase
    {
        public int DisplayID { get; set; }
        public int SelectFlag { get; set; }
    }

    [Serializable]
    public class ProjectInfoBase
    {
        public int AutoID { get; set; }
        public string GUID { get; set; }
        public string ProjectName { get; set; }
        public string ProjectDisk { get; set; }
        public DateTime CreateTime { get; set; }
        public string Description { get; set; }
        public int DeleteFlag { get; set; }
    } 
}